package com.kefu.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.exam.mapper.ExamPaperSubjectMapper;
import com.kefu.exam.mapper.ExamSubjectAnswerMapper;
import com.kefu.exam.mapper.ExamSubjectMapper;
import com.kefu.exam.model.ExamPaperSubject;
import com.kefu.exam.model.ExamSubject;
import com.kefu.exam.model.ExamSubjectAnswer;
import com.kefu.exam.service.ExamSubjectAnswerService;
import com.kefu.exam.service.ExamSubjectService;
import com.kefu.exam.vo.*;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xy
 * @date 2022/02/28
 */
@Api(tags = "问题")
@RestController
@RequestMapping("exam/subject")
public class ExamSubjectController extends AbstractBaseController {
    @Resource
    private ExamSubjectService examSubjectService;
    @Resource
    protected ExamPaperSubjectMapper examPaperSubjectMapper;
    @Resource
    protected ExamSubjectMapper examSubjectMapper;
    @Resource
    protected ExamSubjectAnswerMapper examSubjectAnswerMapper;

    @PostMapping("/add")
    @ApiOperation(value = "创建试题", notes = "创建试题")
    public Result<SubjectUnit> addSubject(@Validated @RequestBody ExamSubjectInsertVo vo) {
        logger.info("创建试题:{}", vo);

        return ResultGenerator.genSuccessResult(examSubjectService.addSubject(vo));
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改试题", notes = "修改试题")
    public Result<String> updateSubject(@Validated @RequestBody ExamSubjectInsertVo vo) {
        logger.info("修改试题:{}", vo);

        return ResultGenerator.genSuccessResult(examSubjectService.updateSubject(vo));
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除考题", notes = "删除考题")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除考题表:{}", delete);

        return ResultGenerator.genSuccessResult(examSubjectService.deleteSubject(delete.getId()));
    }

    @PostMapping("/deleteProblem")
    @ApiOperation(value = "删除问卷下的考题", notes = "删除问卷下的考题")
    public Result<String> deleteProblem(@Validated @RequestBody ExamPaperSubjectDeleteVo deleteVo) {
        logger.info("删除考题表:{}", deleteVo);
        return ResultGenerator.genSuccessResult(examSubjectService.deleteExamSubject(deleteVo.getSubjectId(),deleteVo.getPaperId()));
    }

    @PostMapping("/detail")
    @ApiOperation(value = "考题表详情", notes = "考题表详情")
    public Result<ExamSubjectUnitVo> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("考题表查看详情:{}", detail);

        return ResultGenerator.genSuccessResult(examSubjectService.processData(examSubjectService.getSubjectUnit(detail.getId())));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询考题表", notes = "查询考题表")
    public Result<Page<ExamSubject>> list(@Validated @RequestBody ExamSubjectSearchVo search) {
        logger.info("查询考题表:{}", search);
        LambdaQueryWrapper<ExamPaperSubject> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ExamPaperSubject::getPaperId,search.getPaperId());
        List<ExamPaperSubject> examPaperSubjects = examPaperSubjectMapper.selectList(queryWrapper);
        ArrayList<String> list = new ArrayList<>();
        if (examPaperSubjects.size()>0){
            for(ExamPaperSubject paperSubject:examPaperSubjects){
                String subjectId = paperSubject.getSubjectId();
                list.add(subjectId);
            }
        }
        search.setExamSubject(list);
        Page<ExamSubject> page = examSubjectService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }


    @PostMapping("/updateList")
    @ApiOperation(value = "批量修改试题父题目", notes = "批量修改试题父题目")
    public Result<String> updateListSubject(@Validated @RequestBody List<ExamSubjectUpdateListVo> voList) {
        logger.info("批量修改试题父题目:{}", voList);

        return ResultGenerator.genSuccessResult(examSubjectService.updateListSubject(voList));
    }

    @PostMapping("/getSonSubjectList")
    @ApiOperation(value = "获取未关联子题目", notes = "获取未关联子题目")
    public Result<List<ExamSubject>> getSonSubjectList(@Validated @RequestBody ExamSubjectSearchVo vo) {
        logger.info("获取未关联子题目:{}");

        List<String> subjects = new ArrayList<>();
        List<ExamSubject> subjectList = examSubjectMapper.selectList(Wrappers.lambdaQuery(ExamSubject.class)
                .ne(ExamSubject::getFatherSubject, "")
                .or()
                .isNotNull(ExamSubject::getFatherSubject));
        List<ExamSubjectAnswer> examSubjectAnswers = examSubjectAnswerMapper.selectList(null);
        for (ExamSubject examSubject : subjectList) {
            for (ExamSubjectAnswer answer : examSubjectAnswers) {
                if (examSubject.getFatherSubject().equals(answer.getAnswerId())) {
                    subjects.add(answer.getSubjectId());
                }
            }
        }
        LambdaQueryWrapper<ExamSubject> wrapper = vo.getLambdaExample();
        for (String subjectId : subjects) {
            wrapper.ne(ExamSubject::getSubjectId, subjectId);
        }
        wrapper.and(w ->
                w.isNull(ExamSubject::getFatherSubject)
                        .or()
                        .eq(ExamSubject::getFatherSubject, "")
        );
        List<ExamSubject> examSubjectList = examSubjectMapper.selectList(wrapper);
        return ResultGenerator.genSuccessResult(examSubjectList);
    }

    @PostMapping("/selectAnswer")
    @ApiOperation(value = "跳转问题答案", notes = "跳转问题答案")
    public Result<List<ExamSubjectAnswer>> selectAnswer(@Validated @RequestBody  ExamSubjectAnswerJumpVo examSubjectAnswerJumpVo) {
        logger.info("跳转问题答案:{}", examSubjectAnswerJumpVo);

        return ResultGenerator.genSuccessResult(examSubjectService.selectAnswer(examSubjectAnswerJumpVo));
    }

    @PostMapping("/jumpSubject")
    @ApiOperation(value = "跳转到的问题", notes = "跳转到的问题")
    public Result<List<ExamSubject>> jumpSubject(@Validated @RequestBody  ExamSubject examSubject) {
        logger.info("跳转到的问题:{}", examSubject);

        return ResultGenerator.genSuccessResult(examSubjectService.jumpSubject(examSubject));
    }

}
